﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Medianamik.Core;
using Medianamik.Globalization;
using Telerik.Web.UI;

namespace Medianamik.UI.ConfigManagement.UserControls
{
	public partial class MenuGroupList : BaseModuleListUserControl
	{
        protected void Page_Load(object sender, EventArgs e)
        {
            Grid.Rebind();
        }

        protected override void GridNeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            menuGroupGrid.DataSource = ConfigManager.GetAll<MenuGroup>(Page.CurrentUICulture)
                 .OrderBy(m => m.Position);
        }

        protected override RadGrid Grid
        {
            get { return menuGroupGrid; }
        }

        protected override UpdatePanel GridUpdatePanel
        {
            get { return mainUpdatePanel; }
        }

        protected override string ManagementURL
        {
            get { return "MenuGroupManagement.aspx"; }
        }

        protected void MenuGroupList_RowDrop(object sender, GridDragDropEventArgs e)
        {
            if(e.DraggedItems[0].OwnerGridID == Grid.ClientID)
            {
                var movedNode = NodeManager.GetNode((Guid) e.DraggedItems[0].GetDataKeyValue("ID"));
                var destinationNodeId = (Guid) e.DestDataItem.GetDataKeyValue("ID");
                var pos = e.DropPosition == GridItemDropPosition.Above
                        ? DropPosition.Above
                        : DropPosition.Below;
                NodeManager.ChangeNodeOrder(movedNode, destinationNodeId, pos);
                ((MedianamikSiteMapProvider)SiteMap.Provider).Rebuild();
                Grid.Rebind();
            }
        }

        protected void MenuGroupList_DataBound(object sender, GridItemEventArgs e)
        {
            var item = e.Item as GridDataItem;
            if(item != null)
            {
                string name = item.GetDataKeyValue("Name").ToString();
                string id = ((Guid) item.GetDataKeyValue("ID")).ToString("N");
                string desc = Translator.Translate("SiteMap", "menuGroup_Description_" + id);
                var ctrl = new HtmlGenericControl("div");
                if (!string.IsNullOrEmpty(desc))
                {
                    ctrl.Attributes.Add("title", desc);
                    
                }
                ctrl.InnerText = name;
                item["MenuGroupName"].Controls.Add(ctrl);
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            const string script = @"
function onRowDropping(sender, args) {{
    
}}";
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "onRowDropping", string.Format(script, Grid.ClientID), true);
        }
	}
}